﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace SPCleanSolutionStore
{
    class Program
    {
        static void Main(string[] args)
        {
            SPFarm farm = SPFarm.Local;

            Collection<SPWebApplication> apps = new Collection<SPWebApplication>();
            foreach (SPWebApplication app in SPWebService.ContentService.WebApplications)
            {
                apps.Add(app);
            }

            foreach (SPWebApplication app in SPWebService.AdministrationService.WebApplications)
            {
                apps.Add(app);
            }

            foreach (SPSolution sol in farm.Solutions)
            {

                if (sol.ContainsWebApplicationResource)
                {
                    sol.RetractLocal(apps);
                }
                else
                {
                    sol.RetractLocal();
                }
                sol.Delete();
            }

        }
    }
}
